package com.haiyu.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyu.manager.dao.UserAddressInfoMapper;
import com.haiyu.manager.dao.UserInfoMapper;
import com.haiyu.manager.dao.WorkerMapper;
import com.haiyu.manager.dto.UserInfoDTO;
import com.haiyu.manager.dto.UserInfoSearchDTO;
import com.haiyu.manager.pojo.UserAddressInfo;
import com.haiyu.manager.pojo.UserInfo;
import com.haiyu.manager.pojo.Worker;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    UserAddressInfoMapper userAddressInfoMapper;
    @Autowired
    WorkerMapper workerMapper;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public PageDataResult getUserInfoList(UserInfoSearchDTO userInfoSearch, Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        //////通过师傅编号查询
        if(userInfoSearch.getWorkerId()!=null){
            Worker worker=workerMapper.getWorkerById(userInfoSearch.getWorkerId());
            userInfoSearch.setBuyerOpenid(worker.getOpenId());

        }

        List<UserInfoDTO> userInfoDTO = userInfoMapper.getUserInfo(userInfoSearch);
        for(int i=0;i<userInfoDTO.size(); i++)    {
            //地址
            List<UserAddressInfo> userAddressInfo = userAddressInfoMapper.getUserAddressInfo(userInfoDTO.get(i).getBuyerOpenid());
            userInfoDTO.get(i).setUserAddressInfos(userAddressInfo);
            //师傅信息
            ///修改记录
            List<Worker> workers = workerMapper.getWorkers(userInfoDTO.get(i).getBuyerOpenid(),0);
            System.out.println(workers);
            userInfoDTO.get(i).setWorkers(workers);
            ///当前有效的师傅信息
            Worker worker = workerMapper.getWorker(userInfoDTO.get(i).getBuyerOpenid());
            System.out.println(worker);
            userInfoDTO.get(i).setWorker(worker);
            if(worker!=null||workers.size()>0){
                userInfoDTO.get(i).setIsWorker(0);
            }else {
                userInfoDTO.get(i).setIsWorker(1);
            }

        }
        System.out.println("userInfoSearch有" + userInfoSearch);
        System.out.println("userInfoDTO有" + userInfoDTO);
        PageHelper.startPage(pageNum, pageSize);

        if (userInfoDTO.size() != 0) {
            PageInfo<UserInfoDTO> pageInfo = new PageInfo<>(userInfoDTO);
            pageDataResult.setList(userInfoDTO);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public Map<String, Object> delUser(String buyerOpenid, Integer userInfoLabel) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除用户
            int result = userInfoMapper.updateUserLabel( buyerOpenid, userInfoLabel);
            if (result == 0) {
                data.put("code", 0);
                data.put("msg", "账号封禁失败");
                logger.error("账号封禁失败");
                return data;
            }
            data.put("code", 1);
            data.put("msg", "账号封禁成功");
            logger.info("账号封禁成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("账号封禁异常！", e);
        }
        return data;
    }

    @Override
    public Map<String, Object> recoverUser(String buyerOpenid, Integer userInfoLabel) {
        Map<String, Object> data = new HashMap<>();
        try {
            int result = userInfoMapper.updateUserLabel(buyerOpenid, userInfoLabel);
            if (result == 0) {
                data.put("code", 0);
                data.put("msg", "恢复账号失败");
            }
            data.put("code", 1);
            data.put("msg", "恢复账号成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("恢复账号异常！", e);
        }
        return data;
    }

    @Override
    public UserInfo getUserById(Integer userId) {
        return userInfoMapper.selectByPrimaryKey(userId);
    }


}
